package com.audaque.springboot.foshanupload.web.webdemo.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.HashMap;
import java.util.concurrent.*;

@Slf4j
@RestController
@RequestMapping("cpu")
public class CpuController {

    private Object lock1 = new Object();
    private Object lock2 = new Object();

    /**
     * 线程池，大小1
     */
    private static ExecutorService executorService = Executors.newFixedThreadPool(1);
    private static HashMap<Object, Object> myMap = new HashMap<>();


    private static final int BUFFER = 1024 * 1024 * 1024;//1GB

    @RequestMapping("cpuOverFlow")
    public void cpuOverFlow() {
        new Thread(() -> {
            while (true) {
                //线程耗尽，系统假死
                new Thread(() -> {
                }).start();
            }

        }).start();


        //不受影响
        new Thread(() -> {

            while (true) {
                //打断点依然可以进来
                System.out.println(new Date().toString() + Thread.currentThread() + "**");
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }).start();


    }


    /**
     * 普通消耗CPU的线程
     */
    @RequestMapping("cpuNormal")
    private void cpuNormal() {
        for (int i = 0; i < Integer.MAX_VALUE; i++) {
            new Thread(() -> {
                log.debug("cpu start");
            }).start();
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }


    @RequestMapping("deadlock")
    public String deadlock() {
        //好比情侣约会，不管谁先到都的等另一个，这里就是两个线程，
        CyclicBarrier cyclicBarrier = new CyclicBarrier(2);
        //主线程等待各子线程全部执行完毕后再往下执行:
        final CountDownLatch count = new CountDownLatch(2);

        new Thread(() -> {
            synchronized (lock1) {
                try {
                    cyclicBarrier.await();
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                } catch (BrokenBarrierException e) {
                    throw new RuntimeException(e);
                }
                //持有locak1想要lock2
                synchronized (lock2) {
                    System.out.println("thread1 over");
                }
                count.countDown();

            }
        }).start();
        new Thread(() -> {
            synchronized (lock2) {
                try {
                    cyclicBarrier.await();
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                } catch (BrokenBarrierException e) {
                    throw new RuntimeException(e);
                }
                //持有locak2想要lock1
                synchronized (lock1) {
                    System.out.println("thread2 over");
                }
                count.countDown();

            }
        }).start();
        try {
            count.await();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        //死锁，没有释放线程
        System.out.println("All end!!!");


        //都想拿对方的锁
        return "success";
    }


}
